#include "trans.h"


int recvFileFromServer(int sockFd,char *fileName) {
    off_t fileSize = 0;
    // 用户输入gets filename
    // 接收文件大小
    ssize_t ret = recv(sockFd, &fileSize, sizeof(off_t), MSG_WAITALL);
    if (ret != sizeof(off_t)) {
        printf("接收文件大小失败\n");
        return -1;
    }
    printf("文件大小==%jd\n", fileSize);
    
    // 打开文件
    int fileFd = open(fileName, O_RDWR | O_CREAT | O_TRUNC, 0666);
    if (fileFd == -1) {
        perror("打开文件失败");
        return -1;
    }
    
    char buf[1024*1024] = {0};
    off_t received = 0;
    off_t lastPrint = 0;
    
    while (received < fileSize) {
        // 计算剩余需要接收的字节数
        size_t toRead = (fileSize - received) < sizeof(buf) ? (fileSize - received) : sizeof(buf);
        
        // 接收数据
        ssize_t ret = recv(sockFd, buf, toRead, 0);
        if (ret == -1) {
            perror("接收数据失败");
            close(fileFd);
            return -1;
        } else if (ret == 0) {
            // 连接关闭，但文件未接收完
            printf("连接意外关闭，文件未接收完整\n");
            break;
        }
        
        // 写入文件
        ssize_t written = write(fileFd, buf, ret);
        if (written != ret) {
            perror("写入文件失败");
            close(fileFd);
            return -1;
        }
        
        received += ret;
        
        // 每1%更新一次进度
        if (received - lastPrint >= fileSize / 10000 || received == fileSize) {
            printf("\r进度:%.2f%%", 100.0 * received / fileSize);
            fflush(stdout);
            /* usleep(50000); */
            lastPrint = received;
        }
    }
    
    printf("\r进度:100.00%%\n");
    printf("实际接收%jd\n", received);
    
    // 检查是否完整接收
    if (received != fileSize) {
        printf("错误：接收的文件大小不匹配，期望%jd，实际%jd\n", fileSize, received);
        close(fileFd);
        return -1;
    }
    
    close(fileFd);
    return 0;
}
int sendFileToServer(int sockFd,char *fileName){
    //通过管道向服务端发送文件 puts fileName
    printf("文件名%s\n",fileName);
    struct stat fileStat;
    int fileFd=open(fileName,O_RDWR);
    ERROR_CHECK(fileFd,-1,"file open");
    fstat(fileFd,&fileStat);
    send(sockFd,&fileStat.st_size,sizeof(off_t),0);
    //文件长度
    off_t offset=0;
    size_t onceSentData=0;
    while(offset<fileStat.st_size){
        onceSentData=sendfile(sockFd,fileFd,&offset,1024*1024);
        if(onceSentData>fileStat.st_size/10000){
            printf("\r进度:%.2f%%",100.0*offset/fileStat.st_size);
            fflush(stdout);
            /* usleep(50000); */
        }
    }
    printf("\r进度:100%%\n");
    close(fileFd);
    return 0;
}

int sendBigFileToServer(int sockFd,char *fileName){
    size_t fileSize=strlen(fileName);
    struct stat fileStat;
    int fileFd=open("file1",O_RDWR);
    ERROR_CHECK(fileFd,-1,"file open");
    fstat(fileFd,&fileStat);
    send(sockFd,&fileSize,sizeof(size_t),0);
    //文件名长度
    send(sockFd,fileName,fileSize,0);
    //文件名
    send(sockFd,&fileStat.st_size,sizeof(off_t),0);
    sendfile(sockFd,fileFd,NULL,fileStat.st_size);
    close(fileFd);
    return 0;
}
//int recvFileFromWorker(usrInfo_t *usrInfo){
//    size_t fileNameSize=0;
//    off_t fileSize=0;
//    //收文件名长度
//    recv(usrInfo->netFd,&fileNameSize,sizeof(size_t),0);
//    printf("文件名长度=%ld\n",fileNameSize);
//    char *fileName=(char *)calloc(fileNameSize+1,sizeof(char));
//    //收文件名
//    recv(usrInfo->netFd,fileName,fileNameSize,MSG_NOSIGNAL);
//    fileName[fileNameSize]='\0';
//    printf("%s\n",fileName);
//    //收文件长度
//    recv(usrInfo->netFd,&fileSize,sizeof(off_t),MSG_NOSIGNAL);
//    printf("文件大小==%ld\n",fileSize);
//    int fileFd=open(fileName,O_RDWR|O_CREAT|O_TRUNC,0666);
//    size_t onceSentData=0;
//    char buf[4096]={0};
//    for(size_t sentData=0;sentData<fileSize;sentData += onceSentData){
//        bzero(buf,sizeof(buf));
//        onceSentData=recv(usrInfo->netFd,buf,sizeof(buf),MSG_NOSIGNAL);
//        if(onceSentData==0){
//            break;
//        }
//        if(onceSentData>fileSize/10000){
//            printf("\r进度:%.2f%%",100.0*sentData/fileSize);
//            fflush(stdout);
//            usleep(50000);
//        }
//    }
//    printf("\r进度:100.00%%\n");
//    close(fileFd);
//    return 0;
//}
